package cn.sccl.dirms.security.service.impl;

import cn.sccl.common.dao.BaseDAO;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.service.impl.BaseManagerImpl;
import cn.sccl.dirms.security.dao.UserDAO;
import cn.sccl.dirms.security.model.User;
import cn.sccl.dirms.security.model.UserExtend;
import cn.sccl.dirms.security.model.UserPrincipal;
import cn.sccl.dirms.security.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@Service
public class UserManagerImpl extends BaseManagerImpl<User> implements UserManager {

    @Autowired
    private UserDAO userDAO;

    public UserDAO getUserDAO() {
        return userDAO;
    }

    @Override
    public BaseDAO<User> getBaseDAO() {
        return getUserDAO();
    }

    @Override
    public User findByName(String name) {
        Map queryMap = new HashMap();
        queryMap.put("name", name);
        return this.findUniqueBy(queryMap);
    }

    @Override
    public User getUser(String id) {
        String sql = "select u.*\n" +
                "        from t_sec_user u\n" +
                "        where u.id=?";
        List<User> usersList = getDataBase().query4Model(sql, User.class, new Object[]{id});
        if(usersList == null || usersList.isEmpty()){
            return null;
        }
        User user = usersList.get(0);
        return user;
    }

    @Override
    public List<User> findUserDataByName(String name) {
        Map queryMap = new HashMap();
        queryMap.put("name", name);
        return this.findByLike(queryMap);
    }

    @Override
    public List<User> findUserOfRole(String name, String id) {
        String sql = "select u.id,u.name,u.user_name\n" +
                "    from   t_sec_user     u,\n" +
                "       t_sec_users_roles      a\n" +
                " where  u.name like ?\n" +
                "   and  a.role_id = ?\n" +
                "   and  a.user_id = u.id\n" +
                "   and  u.delete_flag = 0\n" +
                "   and  a.delete_flag = 0";
        List<User> userList = getDataBase().query4Model(sql, User.class, new Object[]{name+'%', id});
        HashSet<User> hs = new HashSet<User>(userList);//通过hashset来去除list中重复的元素
        userList.clear();
        userList.addAll(hs);
        return userList;
    }

    @Override
    public List<UserPrincipal> findUserPrincipal(String userName) {
        List params = new ArrayList();
        String sql ="select u.id,u.user_Name from t_sec_user u";
        if(userName != null){
            sql +=" where upper(u.name) like ? ";
            params.add("%"+userName.toUpperCase()+"%");
        }
        sql+=" order by u.name";
        List<UserPrincipal> userList = getDataBase().query4Model(sql, UserPrincipal.class,params.toArray());
        return userList;
    }

    @Override
    public List<User> findUserData(String id) {
        String sql = "select DISTINCT u.id,u.name,u.user_Name\n" +
                "  from t_sec_role       r,\n" +
                "       t_sec_user     u,\n" +
                "       t_sec_users_roles      a\n" +
                " where  a.role_id = ?\n" +
                "   and  a.user_id = u.id\n" +
                "   and  u.delete_flag = 0\n" +
                "   and  a.delete_flag = 0";
        List<User> userList = getDataBase().query4Model(sql, User.class, new Object[]{id});
        HashSet<User> hs = new HashSet<User>(userList);//通过hashset来去除list中重复的元素
        userList.clear();
        userList.addAll(hs);
        return userList;
    }

    @Override
    public PageResult<UserExtend> findExistUserDataOfRole(String id, long pageIndex, int pageSize) {
        String sql = "select DISTINCT u.id,u.name,u.user_Name," +
                "   a.id as ruid ,a.role_id as roleid\n" +
                "  from t_sec_role       r,\n" +
                "       t_sec_user     u,\n" +
                "       t_sec_users_roles      a\n" +
                " where  a.role_id = ?\n" +
                "   and  a.user_id = u.id\n" +
                "   and  u.delete_flag = 0\n" +
                "   and  a.delete_flag = 0";

        Object[] params= new Object[]{id};
        return  getDataBase().query4Model(sql,pageIndex,pageSize,UserExtend.class,params);
    }

    @Override
    public PageResult<User> findAvailableUserDataOfRole(String id, long pageIndex, int pageSize) {

        String sql = "SELECT DISTINCT\n" +
                "	u.id,\n" +
                "	u. NAME ,u.user_Name\n" +
                "FROM\n" +
                "	t_sec_user u\n" +
                "WHERE\n" +
                "	u.delete_flag = 0\n" +
                "AND NOT EXISTS (\n" +
                "	SELECT\n" +
                "		1\n" +
                "	FROM\n" +
                "		(\n" +
                "			SELECT\n" +
                "				DISTINCT u2.id\n" +
                "			FROM\n" +
                "				t_sec_user u2,\n" +
                "				t_sec_role r,\n" +
                "				t_sec_users_roles a\n" +
                "			WHERE\n" +
                "				a.role_id = ?\n" +
                "			AND a.user_id = u2.id\n" +
                "			AND u2.delete_flag = 0\n" +
                "			AND a.delete_flag = 0\n" +
                "		) t\n" +
                "	WHERE\n" +
                "		t.id = u.id\n" +
                ")";
        Object[] params= new Object[]{id};
        return  getDataBase().query4Model(sql,pageIndex,pageSize,User.class,params);

    }
}